package com.yilin.tms.message.controller;


import com.yilin.tms.core.application.PageData;

import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;

import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.entity.message.MessageData;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.message.service.IMessageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 聊天记录控制器
 *
 * @author zhangjinyuan
 * @date 2022/10/26
 */
@RestController
@RequestMapping("/message/chat/")
public class ChatController extends BaseController {

    @Resource
    private IMessageService messageService;

    /**
     * 聊天记录分页查询
     *
     * @param query 查询条件不放query字段中，直接放在对象的属性里
     */
    @PostMapping(value = "getChatPage")
    @NotNull({"start", "limit"})
    public ReturnData<PageData<MessageData>> getChatPage(MessageData query, @Explain("页码") Integer page,
                                                         @Explain("条数") Integer limit) {
        PageData<MessageData> pageData = messageService.getChatPage(query, page, limit);
        return jsonView(true, "获取成功", pageData);
    }

    /**
     * 修改聊天内容
     */
    @PostMapping(value = "editChat")
    @NotNull("id")
    public ReturnData<Object> editChat(MessageData messageData) {
        messageService.saveOrUpdate(messageData);
        return jsonView(true, "修改消息成功");
    }

    /**
     * 根据发送日期删除消息记录
     */
    @PostMapping(value = "chatDeleteByDate")
    @NotNull({"startDate", "endDate"})
    public ReturnData<Object> chatDeleteByDate(@Explain("开始日期") String startDate, @Explain("结束日期") String endDate) {
        messageService.deleteByDate(startDate, endDate);
        return jsonView(true, "删除消息成功");
    }

    /**
     * 根据id数组删除数据
     */
    @PostMapping(value = "chatDeleteByIds")
    @NotNull("ids")
    public ReturnData<Object> chatDeleteByIds(@Explain("ID数组") String[] ids) {
        messageService.deleteByIds(ids);
        return jsonView(true, "删除消息成功");
    }
}
